﻿using Medianamik.Core.Index;

namespace Medianamik.Core.Action
{
	public class IndexActionHandler : IActionHandler 
	{

		#region IActionHandler Members

        public void Initialize(ActionManager events)
        {
            events.AfterCreateNodeInstance += events_AfterSaveNodeInstance;
            events.AfterUpdateNodeInstance += events_AfterSaveNodeInstance;
            events.BeforeDeleteNodeInstance += events_BeforeDeleteNodeInstance;
        }

       
        public string Description { get { return "Maintient l'index des Noeuds à jour (Lucene)"; } } 
		#endregion

        void events_AfterSaveNodeInstance(ActionEventArgs<NodeInstance> args)
        {
            if (args.Item.Node.NodeType.Indexable)
            {
               new Indexer().Index(args.Item, new DocumentFactory() );
            }
        }

        void events_BeforeDeleteNodeInstance(ActionEventArgs<NodeInstance> args)
        {
            new  Indexer().Remove(args.Item.Node.ID, args.Item.Culture);
        }
       
	}
}
